/*
 * oemv3_msg.h
 *
 *  Created on: 2012. 3. 8.
 *      Author: root
 */

#ifndef OEMV3_MSG_H_
#define OEMV3_MSG_H_


#ifndef NFREQ
#define NFREQ       2                   /* number of carrier frequencies */
#endif
#define NFREQGLO    2                   /* number of carrier frequencies of GLONASS */

//#define	M_ST_BD_SIZE1	4096
#define	M_ST_BD_SIZE1	4096
#define M_ST_BD_SIZE2	M_ST_BD_SIZE1
#define M_ST_OBS_SIZE	(20)
#define STRING_NUM  300
#define iCVType() CONFIG.iCVType==0?"Master":"Slave"
#define HowOEMV()	((fd==fdEB)?"OEMV2":"OEMV1")

#define SDATA 1    // OEM3 data
#define MDATA 0			//UDP input Data
#define STRNUM 500

static double gpst0[]={1980,1, 6,0,0,0}; /* gps time reference */
static double gst0 []={1999,8,22,0,0,0}; /* galileo system time reference */


int oemv3_msg_rcvd( char* buf, int offset, int len, int id, int fd);

typedef struct {        /* time struct */
    time_t time;        /* time (s) expressed by standard time_t */
    double sec;         /* fraction of second under 1 s */
} gtime_t;


typedef struct {        /* GPS/QZS/GAL broadcast ephemeris type */
    int sat;            /* satellite number */
    int iode,iodc;      /* IODE,IODC */
    int sva;            /* SV accuracy (URA index) */
    int svh;            /* SV health (0:ok) */
    int week;           /* GPS/QZS: gps week, GAL: galileo week */
    int code;           /* GPS/QZS: code on L2, GAL: data sources */
    int flag;           /* GPS/QZS: L2 P data flag */
    gtime_t toe,toc,ttr; /* Toe,Toc,T_trans */
                        /* SV orbit parameters */
    double A,e,i0,OMG0,omg,M0,deln,OMGd,idot;
    double crc,crs,cuc,cus,cic,cis;
    double toes;        /* Toe (s) in week */
    double fit;         /* fit interval (h) */
    double f0,f1,f2;    /* SV clock parameters (af0,af1,af2) */
    double tgd[4];      /* group delay parameters */
                        /* GPS/QZS:tgd[0]=TGD */
                        /* GAL    :tgd[0]=BGD E5a/E1,tgd[1]=BGD E5b/E1 */
} eph_t;


typedef struct {        /* observation data record */
    gtime_t time;       /* receiver sampling time (GPST) */
    unsigned char sat,rcv; /* satellite/receiver number */
    unsigned char SNR [NFREQ]; /* signal strength (0.25 dBHz) */
    unsigned char LLI [NFREQ]; /* loss of lock indicator */
    unsigned char code[NFREQ]; /* code indicator (CODE_???) */
    double L[NFREQ];    /* observation data carrier-phase (cycle) */
    double P[NFREQ];    /* observation data pseudorange (m) */
    float  D[NFREQ];    /* observation data doppler frequency (Hz) */
} obsd_t;

typedef struct {        /* observation data */
    int n,nmax;         /* number of obervation data/allocated */
    obsd_t *data;       /* observation data records */
} obs_t;


typedef struct
{
	double x;
	double y;
	double z;
	double corr_clk;
	double corr_ion;
	double corr_trop;
}_st_sat_xyz;


typedef struct
{
	gtime_t time;
	int b[32+1];
	int b_corr[32+1];
	obsd_t obs[32+1];
//    obsd_t diff[32+1];
    _st_sat_xyz corr[32+1];
}_st_obs_data;



typedef struct
{
	//gtime_t time;
	int iH; //head
	int iT; //tail
	int iObsCnt;
	int iXyzCnt;
	int iHprev;
	int bStopGetData;
	double pos[3]; // ECEF
	double InputCableDelay;
	double  timeInterval[M_ST_OBS_SIZE];
	_st_obs_data d[M_ST_OBS_SIZE];
	//double  c_dt[M_ST_OBS_SIZE];
}_st_obs;

typedef struct
{
	int i;
//	int iHead;
//	int iTail;
	char buf[M_ST_BD_SIZE1];
	char t_buf[M_ST_BD_SIZE2];
}_st_buf_decode;

typedef struct{
	char *clock_status;
	double offset;
	double offset_std;
	double utc_offset;
	long utc_year;
	int utc_month;
	int utc_day;
	int utc_hour;
	int utc_min;
	long utc_ms;
	char *utc_status;
	int gps_status;
}_st_timea;

typedef struct{
	double lat_degree;	//Master WGS84 Latitude in degrees
	double long_degree;	//Master WGS84 Longithde in degrees
	double MSL_height;	//Master MSL Height in metres
	float undulation;		//Undulation in metres
	float lat_std;			//Latitude Std in metres
	float long_std;		//Longitude Std in metres
	float hgt_std;			//Height Std in metres
	char *stn_id;			//Receiver ID
	unsigned char SVs;		//Number of satellite vehicles tracked
	unsigned char solnSVs;	//Number of satellite vehicles used in solution
	unsigned char obs;		//Number of satellite above elevation mask angle
	unsigned char multi;		//Number of satellite above the mask angle with L2
}_st_masterposa;

typedef struct{
	double lat_degree;	//Rover WGS84 Latitude in degrees
	double long_degree;	//Rover WGS84 Longithde in degrees
	double MSL_height;	//Rover MSL Height in metres
	float undulation;		//Undulation in metres
	float lat_std;			//Latitude Std in metres
	float long_std;		//Longitude Std in metres
	float hgt_std;			//Height Std in metres
	char *stn_id;			//Receiver ID
	unsigned char SVs;		//Number of satellite vehicles tracked
	unsigned char solnSVs;	//Number of satellite vehicles used in solution
	unsigned char obs;		//Number of satellite above elevation mask angle
	unsigned char multi;		//Number of satellite above the mask angle with L2
}_st_roverposa;

typedef struct{
	double b_x;			//X-axis offset (m)
	double b_y;			//Y-axis offset (m)
	double b_z;			//Z-axis offset (m)
	float b_x_std;		//Standard deviation of B-X (m)
	float b_y_std;		//Standard deviation of B-Y (m)
	float b_z_std;		//Standard deviation of B-Z (m)
	char *stn_id;		//Base station identification
	unsigned char SVs;			//Number of satellite vehicles tracked
	unsigned char solnSVs;		//Number of satellite vehicles used in solution
	unsigned char ggL1;			//Number of GPS plus GLONASS L1 used in solution
	unsigned char ggL1L2;		//Number of GPS plus GLONASS L1 and L2 used in solution
	unsigned char ext_sol_stat;//Extended solution status(see Table 53, Extended Solution Status on page 254)
	unsigned char sig_mask;		//Signals used mask - if 0, signals used in solution are unknown (see Table 52 on page 254)
}_st_bslnxyza;

typedef struct{
	double heading;		//Heading in degrees
	unsigned char True;	//Degrees True
}_st_gphdt;

typedef struct{
	char *sol_stat;		//Solution status
	float base_length;	//Baseline length (0 to 3000 m)
	float heading;			//Heading in degrees (0 to 360.0 degrees)
	float pitch;			//Pitch (±90 degrees)
	float hdg_std_dev;	//Heading standard deviation in degrees
	float pitch_std;		//Pitch standard deviation in degrees
	char *stn_id;			//Station ID string
	unsigned char SVs;		//Number of satellite tracked
	unsigned char solnSVs;	//Number of satellite in solution
	unsigned char obs;		//Number of satellite above the elevation mask
	unsigned char multi;		//Number of satellite above the mask angle with L2
	unsigned char ext_sol_stat;//Extended solution status (see Table 53, Extended	Solution Status on page 254)
	unsigned char sig_mask;		//Signals used mask - if 0, signals used in solution are	unknown (see Table 52 on page 254)
}_st_heading;

typedef struct{
	_st_masterposa masterposa;
	_st_roverposa roverposa;
	_st_bslnxyza bslnxyza;
	_st_gphdt gphdt;
	_st_heading heading;
}_st_align;


extern _st_buf_decode st_bd[2];  // buf decode
extern _st_obs myObs[2];
extern _st_timea timea;
extern _st_align align;

extern int SATXYZA_FINE;

extern int oemv3_msg_rcvd( char* buf, int offset, int len, int id, int fd);
extern int parse_string(char *cmdline, char *delim, char **argv);
void decode_rcupos_a(int argc, char *argv[],int id,int fd);
void init_st_bd();
int decode_oemv_message(char *buf,int id,int fd);


#define dbgCV(x)	if(iLsiDebugVariable==x)
double time2gpst(gtime_t t, int *week);
gtime_t epoch2time(double *ep);

#endif /* OEMV3_MSG_H_ */
